<template>
  <el-dialog :visible.sync="loginDialogVisible" @close="closeDialog"
             style="width:840px; margin: 20px auto;">
    <h2 class="title">登录</h2>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm"
             style="width: 90%; margin: 0px auto;">
      <!--class="demo-ruleForm login-container">-->
      <el-form-item prop="username" label="用户名">
        <el-input type="text" v-model="ruleForm.username" auto-complete="on" width="100%"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" v-model="ruleForm.password" auto-complete="off" width="100%"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio label="customer"></el-radio>
          <el-radio label="waiter"></el-radio>
          <el-radio label="manager"></el-radio>
        </el-radio-group>
      </el-form-item>
      <!--<el-checkbox v-model="checked"  class="remember">记住密码</el-checkbox>-->
      <el-form-item>
        <el-button style="width:47%; float: left" @click="cancel">重置</el-button>
        <el-button type="primary" style="width:47%; float:right" @click="login"
                   :loading="logining">确定
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>

export default {
  name: "loginDialog",
  components: {
  },
  data() {
    return {
      loginDialogVisible: false,
      logining: false,
      token: '',
      type:'',
      ruleForm: {
        userId:'',
        username: '',
        password: '',
        type:''
      },
      rules: {
        username: [
          {required: true, message: '用户名不能为空', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
        ]
      },
    };
  },
  methods: {

    cancel() {
      this.$refs.ruleForm.resetFields();
    },

    closeDialog() {
      this.loginDialogVisible = false;
      this.$refs.ruleForm.resetFields();
    },
    login() {
      this.$md5('holle') // bcecb35d0a12baad472fbe0392bcc043 密码加密
      this.logining = true;
      sessionStorage.setItem('type', this.ruleForm.type);
      console.log("ttt")
      this.type = this.ruleForm.type
      console.log("ttt"+this.type)
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.type= this.ruleForm.type
          let that=this;
          let loginParams = {username: that.ruleForm.username, password: that.ruleForm.password, type:that.ruleForm.type};
          //sessionStorage.setItem('type', that.ruleForm.type);
          console.log("ttt")
          console.log("ttt"+that.type)
          that.$axios.post(that.$URL.userlogin, loginParams).then(
              res => {
                if (res.status == 200) {
                  that.$message({
                    message: "用户登录成功",
                    type: 'success'
                  });
                  that.$refs.ruleForm.resetFields();
                  that.logining = false;
                  that.loginDialogVisible = false;
                  this.token=res.data.token
                  sessionStorage.setItem('token', JSON.stringify(res.data.token));
                  console.log("when login type is"+that.ruleForm.type)
                  //sessionStorage.setItem('type', JSON.stringify(that.ruleForm.type));


                  let id
                  //刷新页面，重新判断几个按钮的显示隐藏
                  this.$store.commit('token',this.token)
                  this.$store.commit('type',this.ruleForm.type)
                  console.log(this.$store.getters.role)
                  this.$router.go({path: '/index'});

                } else if (res.status == 500) {
                  that.$refs.ruleForm.resetFields();
                  that.$message({
                    message: "用户信息错误",
                    type: 'error'
                  });
                  that.logining = false;
                }
              }
          );
          let url = this.$URL.userinfo+"?token="+this.token
          console.log("token is"+this.token)
          this.$axios.get(url).then(res=>
          {
            this.userId=res.data.id
            console.log("zhehsi"+this.userId)
            sessionStorage.setItem('userId', res.data.id);
            this.$store.commit('userId',res.data.id)
            debugger;
            //如果当前是登录状态，则显示用户/管理员中心
          }).catch(err=>{})
        }
      });
    },
  },


  created() {
    this.$Bus.$on('showLogin', () => {
      this.loginDialogVisible = true;
    })
  },

}
//md5 str


</script>

<style scoped>
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #505458;
}

.remember {
  margin: 0px 0px 35px 0px;
}

.dialogTitle {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #505458;
}

.el-form-item {
  margin-bottom: 30px;
}

.el-input {
  position: relative;
  font-size: 14px;
  display: inline-block;
  width: 240px;
  float: right;
}

.el-form-item__error {
  color: #F56C6C;
  font-size: 12px;
  line-height: 1;
  padding-top: 4px;
  position: absolute;
  top: 100%;
  left: 31%;
}
</style>
